/*
 * SearchReportPanel.java
 *
 * Created on 29 April 2004, 08:18
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui;

import com.velocityme.client.gui.exception.ExceptionWindow;
import com.velocityme.client.utility.StatelessRemoteClientSessionUtil;
import com.velocityme.interfaces.KeySession;
import com.velocityme.interfaces.NodePK;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import javax.swing.JFileChooser;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author  Robert
 */
public class SearchReportPanel extends javax.swing.JPanel {
    
    private KeySession m_key;
    private NodePK m_rootNodePK;
    private AbstractTableModel m_results;
    private ByteArrayOutputStream m_currentHtmlOutputStream;
    
    /** Creates new form SearchReportPanel */
    public SearchReportPanel(KeySession p_key, NodePK p_rootNodePK, AbstractTableModel p_results) {
        initComponents();
        m_key = p_key;
        m_rootNodePK = p_rootNodePK;
        m_results = p_results;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jScrollPaneReport = new javax.swing.JScrollPane();
        jEditorPaneReport = new javax.swing.JEditorPane();
        jPanel2 = new javax.swing.JPanel();
        jButtonGenerate = new javax.swing.JButton();
        jButtonSave = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabelStateChanges = new javax.swing.JLabel();
        jCheckBoxStateChanges = new javax.swing.JCheckBox();
        jCheckBoxComments = new javax.swing.JCheckBox();

        setLayout(new java.awt.BorderLayout());

        jScrollPaneReport.setViewportView(jEditorPaneReport);

        add(jScrollPaneReport, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButtonGenerate.setText("Generate");
        jButtonGenerate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGenerateActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonGenerate);

        jButtonSave.setText("Save");
        jButtonSave.setEnabled(false);
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jPanel2.add(jButtonSave);

        add(jPanel2, java.awt.BorderLayout.SOUTH);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabelStateChanges.setText("Inclusions:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel3.add(jLabelStateChanges, gridBagConstraints);

        jCheckBoxStateChanges.setText("State Changes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        jPanel3.add(jCheckBoxStateChanges, gridBagConstraints);

        jCheckBoxComments.setText("Comments");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        jPanel3.add(jCheckBoxComments, gridBagConstraints);

        add(jPanel3, java.awt.BorderLayout.NORTH);

    }//GEN-END:initComponents

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                FileOutputStream file = new FileOutputStream(chooser.getSelectedFile());
                file.write(m_currentHtmlOutputStream.toByteArray());
                file.close();
            }
            catch(java.io.IOException e) {
                System.out.println(e.getMessage());
            }
        }        
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonGenerateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateActionPerformed
        // TODO add your handling code here:
        try {
            boolean bStateChanges = jCheckBoxStateChanges.isSelected();
            boolean bComments = jCheckBoxComments.isSelected();
            // create sorted list of results from table model
            Collection results = new ArrayList();
            for (int row = 0; row < m_results.getRowCount(); row++)
                results.add(m_results.getValueAt(row, 6));
            String text = StatelessRemoteClientSessionUtil.getSession().searchReportXML(m_key, m_rootNodePK, results, bStateChanges, bComments);
            Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("searchReport.xsl"));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(text.getBytes());
            Source source = new StreamSource(inputStream);
            m_currentHtmlOutputStream = new ByteArrayOutputStream();
            Result output = new StreamResult(m_currentHtmlOutputStream);
            transformer.transform(source, output);
            jEditorPaneReport.setEditable(false);
            jEditorPaneReport.setContentType("text/html");
            HTMLDocument doc = (HTMLDocument)((HTMLEditorKit)jEditorPaneReport.getEditorKit()).createDefaultDocument();
            doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
            jEditorPaneReport.read(new ByteArrayInputStream(m_currentHtmlOutputStream.toByteArray()), doc);
            jButtonSave.setEnabled(true);
        }
        catch(Exception e) {
            ExceptionWindow.showError(e);
        }        
    }//GEN-LAST:event_jButtonGenerateActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGenerate;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JCheckBox jCheckBoxComments;
    private javax.swing.JCheckBox jCheckBoxStateChanges;
    private javax.swing.JEditorPane jEditorPaneReport;
    private javax.swing.JLabel jLabelStateChanges;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPaneReport;
    // End of variables declaration//GEN-END:variables
    
}
